package com.hnas.ecard.dao.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.persistence.Query;

import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Component;

import com.hnas.core.dao.BaseDaoImpl;
import com.hnas.core.dao.PageBean;
import com.hnas.core.dto.DTOPagedList;
import com.hnas.core.util.DateUtil;
import com.hnas.core.util.StringUtil;
import com.hnas.ecard.dao.MerchantDetailDao;
import com.hnas.ecard.mapper.MerchantDetailMapper;
import com.hnas.ecard.orm.MerchantDetail;
import com.hnas.ecard.service.dto.DTOMerchantDetail;
import com.hnas.ecard.service.search.DTOMerchantDetailSearch;

@Component("merchantDetailDao")
public class MerchantDetailDaoImpl extends BaseDaoImpl<MerchantDetail> implements MerchantDetailDao{

	public MerchantDetailDaoImpl() {
		super(MerchantDetail.class);
	}

	@Override
	public MerchantDetail saveOrUpdate(DTOMerchantDetail dto) {
		MerchantDetail vo = null;
		if(dto != null) {
			if(StringUtil.isNotEmpty(dto.getId())) {
				vo = this.find(dto.getId());
			}
			if(vo == null) {
				vo = new MerchantDetail();
				BeanUtils.copyProperties(dto, vo);
				vo.setCreateTime(DateUtil.dateTimeToString(new Date()));
				vo.setUpdateTime(DateUtil.dateTimeToString(new Date()));
				vo.setIsDeleted(false);
				this.persist(vo);
			}else {
				BeanUtils.copyProperties(dto, vo,new String[]{"id", "createTime"});
				vo.setUpdateTime(DateUtil.dateTimeToString(new Date()));
				this.merge(vo);
			}
		}
		return vo;
	}

	@Override
	public DTOPagedList<DTOMerchantDetail> page(DTOMerchantDetailSearch criteria) {
		DTOPagedList<DTOMerchantDetail> pageList = new DTOPagedList<DTOMerchantDetail>();
		List<DTOMerchantDetail> list = new ArrayList<DTOMerchantDetail>();
		PageBean<MerchantDetail> page = new PageBean<MerchantDetail>();
		page.setPageNo(criteria.getPageNo());
		page.setPageSize(criteria.getPageSize());
		page.setTotalCount(criteria.getTotalCount());
		StringBuffer sb = new StringBuffer();
		List<Object> paras = new ArrayList<Object>();
		int j = 1;
		if(StringUtil.isNotEmpty(criteria.getName())){
			sb.append(" and t.name like ?"+j++);
			paras.add("%"+criteria.getName().trim()+"%");
		}
		if(StringUtil.isNotEmpty(criteria.getAddress())){
			sb.append(" and t.address like ?"+j++);
			paras.add("%"+criteria.getAddress().trim()+"%");
		}
		if(StringUtil.isNotEmpty(criteria.getMerchantId())){
			sb.append(" and t.merchantId = ?"+j++);
			paras.add(criteria.getMerchantId().trim());
		}
		if(StringUtil.isNotEmpty(criteria.getRemark())){
			sb.append(" and t.getRemark like ?"+j++);
			paras.add("%"+criteria.getRemark().trim()+"%");
		}
		if(sb.length() > 0){
			sb.delete(0, 5);
			sb.insert(0, " from MerchantDetail t  where ");
		}else{
			sb.append(" from MerchantDetail t ");
		}
		sb.append(" order by t.createTime desc");
		page.setQuery(sb.toString());
		page.setQueryCount("select count(t.id) "+sb);
		this.queryByPage(page, paras.toArray());
		pageList.setPageNo(page.getPageNo());
		pageList.setPageSize(page.getPageSize());
		pageList.setTotalCount(page.getTotalCount());
		for(MerchantDetail vo : page.getList()){
			DTOMerchantDetail dto = new DTOMerchantDetail();
			dto = MerchantDetailMapper.getInstance().mapToValue(vo);
			list.add(dto);
		}
		pageList.setList(list);
		return pageList;
	}

	@Override
	public DTOMerchantDetail findById(String id) {
		MerchantDetail vo = this.entityManager.find(MerchantDetail.class, id);
		return MerchantDetailMapper.getInstance().mapToValue(vo);
	}

	@Override
	public void deleteById(String id) {
		String sql = "delete from tbl_merchant_detail where id = ?1";
		Query query = this.entityManager.createNativeQuery(sql);
		query.setParameter(1, id);
		query.executeUpdate();
	}

}
